package com.tangkun.environment.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.RequestBody;
import com.tangkun.environment.R;
import com.tangkun.environment.adapter.TiJiaoXunChaShuJuAdapter;
import com.tangkun.environment.entity.base.CommonReturn;
import com.tangkun.environment.entity.gongzuo.CheckBoxBean;
import com.tangkun.environment.entity.gongzuo.InspectViewReturn;
import com.tangkun.environment.entity.personcenter.UploadResourceReturn;
import com.tangkun.environment.network.MySubscribe;
import com.tangkun.environment.rx.RxActivityHelper;
import com.tangkun.environment.utils.GsonUtil;
import com.tangkun.environment.utils.JumpHelper;
import com.tangkun.environment.utils.LogUtils;
import com.tangkun.environment.utils.Utils;
import com.tangkun.environment.view.CommonQueDingDialog;
import com.tangkun.environment.view.MyListView;
import com.tangkun.environment.view.ShenHeDialog;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.qqtheme.framework.picker.DatePicker;

/**
 * 巡查数据列表 提交数据
 */

public class TiJiaoXunChaShuJuActivity extends BaseActivity implements TiJiaoXunChaShuJuAdapter.MyClickListener {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.next)
    TextView next;
    @BindView(R.id.custom_list_view)
    MyListView customListView;
    private Context mContext;

    /**
     * 企业ID
     */
    private String enterpriseid = "";
    /**
     * 从巡查数据列表过来的需要拼接上这个参数
     */
    private String inspectid = "";
    /**
     * 审核ids  通过或驳回
     */
    private String shenheIds = "";
    /**
     * 巡查数据ID
     */
    private String ids = "";

    /**
     * 日期选择器
     */
    private DatePicker datePicker;
    /**
     * 最大图片上传张数 动态列表图片
     */
    private static final int IMAGE_NUM_DONGTAILIEBIAO_IMG = 9;
    private static final int IMAGE_W = 400;
    private static final int IMAGE_H = 200;
    private static final int CAPTURE_IMAGE = 1;
    public static final int REQUEST_CODE_TEMPLATELIST_SELECTED_IMG = 6;// 请求码 templatelist 选中图片
    /**
     * 完成上传图片 动态图片
     */
    private boolean uploadImgDongtai = false;
    /**
     * 已上传的图片，逗号拼接 动态图片
     */
    private String imgDongtai = "";
    /**
     * ids 动态图片
     */
    private String idsgDongtai = "";

    /**
     * 数据源
     */
    private List<InspectViewReturn.DataBean.TemplatelistBean> dataList = new ArrayList<InspectViewReturn.DataBean.TemplatelistBean>();
    /**
     * 适配器
     */
    private TiJiaoXunChaShuJuAdapter adapter;
    /**
     * 请求码 动态列表checkbox复选框
     */
    private final int REQUEST_CODE_CHECKBOX = 7;
    /**
     * 请求码 动态列表受检单位签章
     */
    private final int REQUEST_CODE_IMGSIGN = 8;
    /**
     * 是否已经提交过数据了,避免重复网络请求
     */
    private boolean hasPosted = false;


    /**
     * 0：查看数据 1：编辑数据 2：提交数据 3:审核
     */
    private int type = 2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        enterpriseid = getIntent().getStringExtra("id");
        inspectid = getIntent().getStringExtra("inspectid");
        shenheIds = getIntent().getStringExtra("shenheIds");
        type = getIntent().getIntExtra("type", 2);
        setContentView(R.layout.activity_tijiaoxunchashuju);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        initView();
        title.setText("");
        switch (type) {
            case 0://查看数据
                next.setText("");
                break;
            case 1://编辑数据
                next.setText("保存");
                break;
            case 2://提交数据
                next.setText("提交审核");
                break;
            case 3://审核
                next.setText("审核");
                break;
        }
        inspectView();
    }

    private void initView() {
        adapter = new TiJiaoXunChaShuJuAdapter(TiJiaoXunChaShuJuActivity.this, dataList);
        adapter.setMyClickListener(this);
        adapter.setId(enterpriseid);//设置企业ID，用于编辑企业资料
        customListView.setAdapter(adapter);
    }

    @OnClick({R.id.back, R.id.next})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                JumpHelper.finish(mContext);
                break;
            case R.id.next:
                switch (type) {
                    case 0://查看数据

                        break;
                    case 1://编辑数据
                        judgeEmpty();
                        break;
                    case 2://提交数据
                        judgeEmpty();
                        break;
                    case 3://审核
                        //审核对话框
                        ShenHeDialog shenHeDialog = new ShenHeDialog(TiJiaoXunChaShuJuActivity.this);
                        shenHeDialog.show();
                        shenHeDialog.setOnClickListener(new ShenHeDialog.OnClickListener() {
                            @Override
                            public void access(String content) {
                                //通过审核
                                inspectflowAudit(shenheIds, 1, content);
                            }

                            @Override
                            public void refuse(String content) {
                                //打回重申
                                inspectflowAudit(shenheIds, 2, content);
                            }
                        });
                        break;
                }
                break;
        }
    }

    /**
     * 巡查数据审核
     *
     * @param ids         流程id
     * @param status      是否通过，1 通过；2 未通过
     * @param description 可选 描述
     */
    private void inspectflowAudit(String ids, int status, String description) {
        Map<String, String> map = new HashMap<>();
        map.put("ids", ids);
        map.put("status", status + "");
        map.put("description", description);
        fromNetwork.inspectflowAudit(map)
                .compose(new RxActivityHelper<CommonReturn>().ioMain(TiJiaoXunChaShuJuActivity.this, true))
                .subscribe(new MySubscribe<CommonReturn>(mContext, true) {
                    @Override
                    protected void success(CommonReturn resultBean) {
                        //tangkun 发出通知 刷新基础数据和巡查数据
                        EventBus.getDefault().post(true, "QiYeZhongXinActivity.refresh");
                        Utils.hideSoftKeyboard(TiJiaoXunChaShuJuActivity.this, back);
                        JumpHelper.finish(mContext);
                    }

                    @Override
                    protected void onMyError(String errorMessage) {
                        showToast(errorMessage);
                    }
                });
    }

    /**
     * 巡查数据列表
     */
    private void inspectView() {
        Map<String, String> map = new HashMap<>();
        map.put("enterpriseid", enterpriseid);
        if (TextUtils.isEmpty(inspectid)) {//提交巡查数据的接口
            fromNetwork.inspectView(map).compose(new RxActivityHelper<InspectViewReturn>().ioMain(TiJiaoXunChaShuJuActivity.this, true))
                    .subscribe(new MySubscribe<InspectViewReturn>(mContext, true) {
                        @Override
                        protected void success(InspectViewReturn resultBean) {
                            if (resultBean.data != null) {
                                //巡查数据ID
                                ids = resultBean.data.ids;

                                if (!TextUtils.isEmpty(resultBean.data.name)) {
                                    title.setText(resultBean.data.name);
                                } else {
                                    title.setText("");
                                }

                                dataList.clear();
                                //填充数据
                                dataList.addAll(resultBean.data.templatelist);
                                //刷新列表
                                adapter.notifyDataSetChanged();
                            }
                        }

                        @Override
                        protected void onMyError(String errorMessage) {
                            showToast(errorMessage);
                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    //关闭页面
                                    JumpHelper.finish(TiJiaoXunChaShuJuActivity.this);
                                }
                            }, 1000);
                        }
                    });
        } else {//巡查数据列表过来的接口
            fromNetwork.inspectViewWithInspectid(map, inspectid).compose(new RxActivityHelper<InspectViewReturn>().ioMain(TiJiaoXunChaShuJuActivity.this, true))
                    .subscribe(new MySubscribe<InspectViewReturn>(mContext, true) {
                        @Override
                        protected void success(InspectViewReturn resultBean) {
                            if (resultBean.data != null) {
                                //巡查数据ID
                                ids = resultBean.data.ids;

                                if (!TextUtils.isEmpty(resultBean.data.name)) {
                                    title.setText(resultBean.data.name);
                                } else {
                                    title.setText("");
                                }

                                dataList.clear();
                                //填充数据
                                dataList.addAll(resultBean.data.templatelist);
                                //刷新列表
                                adapter.notifyDataSetChanged();
                            }
                        }

                        @Override
                        protected void onMyError(String errorMessage) {
                            showToast(errorMessage);
                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    //关闭页面
                                    JumpHelper.finish(TiJiaoXunChaShuJuActivity.this);
                                }
                            }, 1000);
                        }
                    });
        }
    }

    /**
     * 判断图片是否都上传完成了 都上传完成后才调用修改企业接口
     */
    private void judgeEmpty() {
        //待上传图片 动态图片
        if (!uploadImgDongtai) {
            if (dataList == null || dataList.size() <= 0) {
                uploadImgDongtai = true;
            } else {
                for (InspectViewReturn.DataBean.TemplatelistBean bean : dataList) {
                    if (bean.itemlist != null && bean.itemlist.size() > 0) {
                        for (InspectViewReturn.DataBean.TemplatelistBean.ItemlistBean itemBean : bean.itemlist) {
                            //是图片并且有图片
                            if (!TextUtils.isEmpty(itemBean.type) && itemBean.type.equals("img") && !TextUtils.isEmpty(itemBean.value)) {
                                List<String> urlList = Arrays.asList(itemBean.value.split(","));
                                for (String url : urlList) {
                                    if (url.endsWith(".png") || url.endsWith(".jpg")) {
                                        //网络图片
                                        //这是已上传过的图片，有服务器返回的图片地址
                                        //判断动态图片是否全部上传完成
                                        if (isAllDongtaiImgUploaded()) {
                                            uploadImgDongtai = true;
                                            //全部动态图片上传完成，更新企业信息
                                            inspectUpdate();
                                        }
                                    } else {
                                        //本地图片
                                        //这条图片item对应ID
                                        String ids = itemBean.ids;
                                        sendPhotoToServerDongtaiImg(ids, url);
                                    }
                                }
                            } else {
                                //判断动态图片是否全部上传完成
                                if (isAllDongtaiImgUploaded()) {
                                    uploadImgDongtai = true;
                                    //全部动态图片上传完成，更新企业信息
                                    inspectUpdate();
                                }
                            }
                        }
                    } else {
                        //判断动态图片是否全部上传完成
                        if (isAllDongtaiImgUploaded()) {
                            uploadImgDongtai = true;
                            //全部动态图片上传完成，更新企业信息
                            inspectUpdate();
                        }
                    }
                }
            }
        }
        inspectUpdate();
    }

    /**
     * 更新企业资料
     */
    private void inspectUpdate() {
        if (hasPosted) {
            return;
        }
        //所有图片都上传完成后才执行绑定企业操作
        LogUtils.e(" uploadImgDongtai:" + (uploadImgDongtai == true));
        if (uploadImgDongtai == true) {
            Map<String, String> map = new HashMap<>();
            //动态列表中参数
            if (dataList != null && dataList.size() > 0) {
                for (InspectViewReturn.DataBean.TemplatelistBean bean : dataList) {
                    if (bean.itemlist != null && bean.itemlist.size() > 0) {
                        for (InspectViewReturn.DataBean.TemplatelistBean.ItemlistBean itemBean : bean.itemlist) {
                            //把ids当作key，value当作value提交
                            String ids = itemBean.ids;
                            String value = itemBean.value;
                            if (value == null)
                                value = "";
                            LogUtils.e("提交巡查数据参数 ids:" + ids + " value:" + value);
                            map.put(ids, value);
                        }
                    }
                }
            }

            if (TextUtils.isEmpty(ids)) {
                showToast("巡查数据ID为空");
                return;
            }

            //加个双重保险
            hasPosted = true;

            fromNetwork.inspectUpdate(ids, map)
                    .compose(new RxActivityHelper<CommonReturn>().ioMain(TiJiaoXunChaShuJuActivity.this, true))
                    .subscribe(new MySubscribe<CommonReturn>(mContext, true) {
                        @Override
                        protected void success(CommonReturn resultBean) {
                            hasPosted = true;
                            showToast("提交成功");
//                            tangkun 发出通知 刷新基础数据和巡查数据
                            EventBus.getDefault().post(true, "QiYeZhongXinActivity.refresh");
                            Utils.hideSoftKeyboard(TiJiaoXunChaShuJuActivity.this, back);
                            JumpHelper.finish(mContext);
                        }

                        @Override
                        protected void onMyError(String errorMessage) {
                            showToast(errorMessage);
                            hasPosted = false;
                        }
                    });
        }
    }

    /**
     * 设置日期
     *
     * @param textView 控件
     */

    private void setDate(final TextView textView) {
        if (datePicker != null && datePicker.isShowing()) {
            datePicker.dismiss();
        } else {
            datePicker = new DatePicker(TiJiaoXunChaShuJuActivity.this);
            //顶部高度
            datePicker.setTopHeight(47);
            //取消按钮颜色
            datePicker.setCancelTextColor(getResources().getColor(R.color.common_tips));
            //取消按钮文字大小
            datePicker.setCancelTextSize(17);
            //确认按钮颜色
            datePicker.setSubmitTextColor(getResources().getColor(R.color.common_green));
            //按下的文字颜色
            datePicker.setPressedTextColor(getResources().getColor(R.color.common_green_80));
            //确认按钮文字大小
            datePicker.setSubmitTextSize(17);
            //顶部标题栏下划线颜色
            datePicker.setTopLineColor(getResources().getColor(R.color.divider_color_80));
            //顶部标题栏下划线高度
            datePicker.setTopLineHeight(1);
            //日期上下分割线颜色
            datePicker.setLineColor(getResources().getColor(R.color.common_other));
            //日期颜色
            datePicker.setTextColor(getResources().getColor(R.color.common_word));
            //控件展示动画
            datePicker.setAnimationStyle(R.style.PopupwindowStyle);
            //年份范围
            datePicker.setRange(1900, 2100);
            //默认选择日期
            datePicker.setSelectedItem(Integer.valueOf(Utils.getCurrentDateYear()), Integer.valueOf(Utils.getCurrentDateMonth()), Integer.valueOf(Utils.getCurrentDateDay()));
            datePicker.setOnDatePickListener(new DatePicker.OnYearMonthDayPickListener() {
                @Override
                public void onDatePicked(String year, String month, String day) {
                    textView.setText(year + "-" + month + "-" + day);
                }
            });
            datePicker.show();
        }
    }

    /**
     * 打开相册 动态列表图片
     */
    private void openXiangceDongtaiImg() {
        Intent intent = new Intent(TiJiaoXunChaShuJuActivity.this, CaptureImageActivity.class);
        intent.putExtra("w", IMAGE_W);
        intent.putExtra("h", IMAGE_H);
        intent.putExtra("custom", false);
        intent.putExtra("module", "weibo");
        //已上传图片数量
        int uploadNum;
        if (!TextUtils.isEmpty(imgDongtai)) {
            uploadNum = imgDongtai.split(",").length;
        } else {
            uploadNum = 0;
        }
        intent.putExtra("num", IMAGE_NUM_DONGTAILIEBIAO_IMG - uploadNum);
        intent.putExtra("isShowOriginal", false);
        startActivityForResult(intent, REQUEST_CODE_TEMPLATELIST_SELECTED_IMG);
    }


    /**
     * 上传图片到服务器 动态图片
     *
     * @param ids      这条动态图片item对应的ID
     * @param filePath 本地图片路径
     */
    private synchronized void sendPhotoToServerDongtaiImg(final String ids, final String filePath) {
        String[] filePathArray = filePath.split("/");
        //文件名
        String fileName = filePathArray[filePathArray.length - 1];
        fileName = fileName + ".png";//加上.png后缀名
        LogUtils.e("文件名称：" + fileName);
        //生成文件
        File file = new File(filePath);
        //组装partMap对象
        Map<String, RequestBody> partMap = new HashMap<>();
        RequestBody fileBody = RequestBody.create(MediaType.parse("image/*"), file);//文件
        partMap.put("file\"; filename=\"" + fileName + "", fileBody);//文件名
        fromNetwork.uploadImageInspect(partMap).compose(new RxActivityHelper<UploadResourceReturn>().ioMain(TiJiaoXunChaShuJuActivity.this, true))
                .subscribe(new MySubscribe<UploadResourceReturn>(mContext, true) {
                    @Override
                    protected void success(UploadResourceReturn uploadResourceReturn) {
                        if (uploadResourceReturn.data != null) {
                            if (uploadResourceReturn.data.size() > 0) {
                                //文件上传成功，返回的图片半路径，拼接成图片集合
                                String url = uploadResourceReturn.data.get(0).url;
                                for (InspectViewReturn.DataBean.TemplatelistBean bean2 : dataList) {
                                    if (bean2.itemlist != null && bean2.itemlist.size() > 0) {
                                        for (InspectViewReturn.DataBean.TemplatelistBean.ItemlistBean itemBean2 : bean2.itemlist) {
                                            //是图片并且有图片
                                            if (!TextUtils.isEmpty(itemBean2.type) && itemBean2.type.equals("img") && !TextUtils.isEmpty(itemBean2.value)) {
                                                List<String> urlList2 = Arrays.asList(itemBean2.value.split(","));
                                                for (String url2 : urlList2) {
                                                    if (url2.endsWith(".png") || url2.endsWith(".jpg")) {
                                                        //网络图片 无视
                                                    } else {
                                                        //本地图片 替换对应ID的本地地址为服务器返回的地址
                                                        LogUtils.e("itemBean2.ids:" + itemBean2.ids + "  ids:" + ids
                                                                + " url2:" + url2 + " filePath:" + filePath);
                                                        if (itemBean2.ids.equals(ids) && url2.equals(filePath)) {
                                                            //替换
                                                            itemBean2.value = itemBean2.value.replace(url2, url);
                                                        }
                                                    }
                                                }
                                            } else {
                                                if (isAllDongtaiImgUploaded()) {
                                                    uploadImgDongtai = true;
                                                    //全部动态图片上传完成，更新企业信息
                                                    inspectUpdate();
                                                }
                                            }
                                        }
                                    } else {
                                        if (isAllDongtaiImgUploaded()) {
                                            uploadImgDongtai = true;
                                            //全部动态图片上传完成，更新企业信息
                                            inspectUpdate();
                                        }
                                    }
                                }

                                if (isAllDongtaiImgUploaded()) {
                                    uploadImgDongtai = true;
                                    //全部动态图片上传完成，更新企业信息
                                    inspectUpdate();
                                }
                            }
                        }
                    }

                    @Override
                    protected void onMyError(String errorMessage) {
                        showToast(errorMessage);
                    }
                });
    }

    /**
     * 是否所有动态图片都上传完成
     *
     * @return
     */
    private synchronized boolean isAllDongtaiImgUploaded() {
        boolean isAllDongtaiImgUploaded = true;
        if (dataList == null || dataList.size() <= 0) {
            isAllDongtaiImgUploaded = true;
        } else {
            for (InspectViewReturn.DataBean.TemplatelistBean bean2 : dataList) {
                if (bean2.itemlist != null && bean2.itemlist.size() > 0) {
                    for (InspectViewReturn.DataBean.TemplatelistBean.ItemlistBean itemBean2 : bean2.itemlist) {
                        //是图片并且有图片
                        if (!TextUtils.isEmpty(itemBean2.type) && itemBean2.type.equals("img") && !TextUtils.isEmpty(itemBean2.value)) {
                            List<String> urlList2 = Arrays.asList(itemBean2.value.split(","));
                            for (String url2 : urlList2) {
                                if (url2.endsWith(".png") || url2.endsWith(".jpg")) {

                                } else {
                                    isAllDongtaiImgUploaded = false;
                                }
                            }
                        } else {

                        }
                    }
                } else {

                }
            }
        }
        return isAllDongtaiImgUploaded;
    }

    /**
     * 动态列表中选择日期
     *
     * @param ids   ids
     * @param value 2001-01-01
     */
    private void showDatePicker(final String ids, final String value) {
        if (datePicker != null && datePicker.isShowing()) {
            datePicker.dismiss();
        } else {
            datePicker = new DatePicker(TiJiaoXunChaShuJuActivity.this);
            //顶部高度
            datePicker.setTopHeight(47);
            //取消按钮颜色
            datePicker.setCancelTextColor(getResources().getColor(R.color.common_tips));
            //取消按钮文字大小
            datePicker.setCancelTextSize(17);
            //确认按钮颜色
            datePicker.setSubmitTextColor(getResources().getColor(R.color.common_green));
            //按下的文字颜色
            datePicker.setPressedTextColor(getResources().getColor(R.color.common_green_80));
            //确认按钮文字大小
            datePicker.setSubmitTextSize(17);
            //顶部标题栏下划线颜色
            datePicker.setTopLineColor(getResources().getColor(R.color.divider_color_80));
            //顶部标题栏下划线高度
            datePicker.setTopLineHeight(1);
            //日期上下分割线颜色
            datePicker.setLineColor(getResources().getColor(R.color.common_other));
            //日期颜色
            datePicker.setTextColor(getResources().getColor(R.color.common_word));
            //控件展示动画
            datePicker.setAnimationStyle(R.style.PopupwindowStyle);
            //年份范围
            datePicker.setRange(1900, 2100);
            //默认选择日期
            if (!TextUtils.isEmpty(value)) {
                String[] dateArray = value.split("-");
                String year = dateArray[0];
                String month = dateArray[1];
                String day = dateArray[2];
                datePicker.setSelectedItem(Integer.valueOf(year), Integer.valueOf(month), Integer.valueOf(day));
            } else {
                datePicker.setSelectedItem(Integer.valueOf(Utils.getCurrentDateYear()), Integer.valueOf(Utils.getCurrentDateMonth()), Integer.valueOf(Utils.getCurrentDateDay()));
            }
            datePicker.setOnDatePickListener(new DatePicker.OnYearMonthDayPickListener() {
                @Override
                public void onDatePicked(String year, String month, String day) {
                    //结果日期
                    String resultDate = year + "-" + month + "-" + day;
                    //日期不同才更新
                    if (!resultDate.equals(value)) {
                        //更新templatelist
                        for (InspectViewReturn.DataBean.TemplatelistBean bean : dataList) {
                            for (InspectViewReturn.DataBean.TemplatelistBean.ItemlistBean itemBean : bean.itemlist) {
                                if (itemBean.ids.equals(ids)) {
                                    itemBean.value = resultDate;
                                    LogUtils.e("resultDate:" + resultDate);
                                }
                            }
                        }
                        LogUtils.e("dataList:" + GsonUtil.GsonToString(dataList));
                        adapter.notifyDataSetChanged();
//                        adapter.updateSingleRow(customListView, idOnMyClick);
                    }
                }
            });
            datePicker.show();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // 刷新来自相册列表选中的图片列表 动态图片
        if (requestCode == REQUEST_CODE_TEMPLATELIST_SELECTED_IMG && resultCode == CaptureImageActivity.CAPTURE) {
            List<File> fileList = (List<File>) data.getSerializableExtra("fileList");
            List<String> urlList = new ArrayList<String>();
            if (fileList != null) {
                for (File file : fileList) {
//                    urlList.add(Uri.fromFile(file).toString());
                    urlList.add(file.getPath());
                }
            }
            //拼接路径
            String value = "";
            //判断是否已有图片
            if (!TextUtils.isEmpty(imgDongtai)) {
                value = imgDongtai;
            }
            for (String url : urlList) {
                if (TextUtils.isEmpty(value)) {
                    value = url;
                } else {
                    value = value + "," + url;
                }
            }
            //更新templatelist
            for (InspectViewReturn.DataBean.TemplatelistBean bean : dataList) {
                for (InspectViewReturn.DataBean.TemplatelistBean.ItemlistBean itemBean : bean.itemlist) {
                    if (itemBean.ids.equals(idsgDongtai)) {
                        itemBean.value = value;
                    }
                }
            }
            adapter.notifyDataSetChanged();
//            adapter.updateSingleRow(customListView, idOnMyClick);
        }
        // 刷新有过删除后的图片列表 动态图片
        if (requestCode == REQUEST_CODE_TEMPLATELIST_SELECTED_IMG && resultCode == DeleteImageActivity.RESULT_CODE_DELETE_CHANGED) {
            List<File> fileList = new ArrayList<File>();
            fileList = (List<File>) data.getSerializableExtra("fileList");
            List<String> resultUrlList = new ArrayList<String>();
            resultUrlList = (List<String>) data.getSerializableExtra("urlList");
            LogUtils.e("resultUrlList:" + resultUrlList);
            List<String> urlList = new ArrayList<String>();
            //添加返回的urlList
            urlList.addAll(resultUrlList);
//            if (fileList != null) {
//                for (File file : fileList) {
////                    urlList.add(Uri.fromFile(file).toString());
//                    urlList.add(file.getPath());
//                }
//            }
            //拼接路径
            String value = "";
            for (String url : urlList) {
                if (TextUtils.isEmpty(value)) {
                    value = url;
                } else {
                    value = value + "," + url;
                }
            }
            LogUtils.e("value:" + value);
            //更新templatelist
            for (InspectViewReturn.DataBean.TemplatelistBean bean : dataList) {
                for (InspectViewReturn.DataBean.TemplatelistBean.ItemlistBean itemBean : bean.itemlist) {
                    LogUtils.e("itemBean.ids:" + itemBean.ids + "  idsgDongtai" + idsgDongtai);
                    if (itemBean.ids.equals(idsgDongtai)) {
                        itemBean.value = value;
                    }
                }
            }
            adapter.notifyDataSetChanged();
//            adapter.updateSingleRow(customListView, idOnMyClick);
        }

        // 动态列表checkbox复选框分类更改
        if (requestCode == REQUEST_CODE_CHECKBOX && resultCode == RESULT_OK) {
            String ids = data.getStringExtra("ids");
            List<CheckBoxBean> checkBoxBeanList = (List<CheckBoxBean>) data.getSerializableExtra("data");
            String value = GsonUtil.GsonToString(checkBoxBeanList);
            for (InspectViewReturn.DataBean.TemplatelistBean bean : dataList) {
                for (InspectViewReturn.DataBean.TemplatelistBean.ItemlistBean itemBean : bean.itemlist) {
                    if (itemBean.ids.equals(ids)) {
                        itemBean.value = value;
                    }
                }
            }
            adapter.notifyDataSetChanged();
//            adapter.updateSingleRow(customListView, idOnMyClick);
        }

        // 动态列表签名文件
        if (requestCode == REQUEST_CODE_IMGSIGN && resultCode == RESULT_OK) {
            List<File> fileList = (List<File>) data.getSerializableExtra("fileList");
            List<String> urlList = new ArrayList<String>();
            if (fileList != null) {
                for (File file : fileList) {
//                    urlList.add(Uri.fromFile(file).toString());
                    urlList.add(file.getPath());
                }
            }
            //拼接路径
            String value = "";
            //判断是否已有图片
            if (!TextUtils.isEmpty(imgDongtai)) {
                value = "";
            }
            for (String url : urlList) {
                if (TextUtils.isEmpty(value)) {
                    value = url;
                } else {
                    value = value + "," + url;
                }
            }
            //更新templatelist
            for (InspectViewReturn.DataBean.TemplatelistBean bean : dataList) {
                for (InspectViewReturn.DataBean.TemplatelistBean.ItemlistBean itemBean : bean.itemlist) {
                    if (itemBean.ids.equals(idsgDongtai)) {
                        itemBean.value = value;
                    }
                }
            }
            adapter.notifyDataSetChanged();
//            adapter.updateSingleRow(customListView, idOnMyClick);
        }
    }

    /**
     * 点击子列表或子子列表传递过来的ID
     */
    private String idOnMyClick = "";

    /**
     * 动态列表中radiobutton监听回调
     */
    @Override
    public void onMyClick(String type, String ids, String value) {
        idOnMyClick = ids;
        LogUtils.e("type:" + type + "   ids：" + ids + "   value：" + value);
        switch (type) {
            case "text"://文本输入框
                //更新templatelist
                for (InspectViewReturn.DataBean.TemplatelistBean bean : dataList) {
                    for (InspectViewReturn.DataBean.TemplatelistBean.ItemlistBean itemBean : bean.itemlist) {
                        if (itemBean.ids.equals(ids)) {
                            itemBean.value = value;
                        }
                    }
                }
//                adapter.notifyDataSetChanged();
                break;
            case "radio"://单选框
                //更新templatelist
                for (InspectViewReturn.DataBean.TemplatelistBean bean : dataList) {
                    for (InspectViewReturn.DataBean.TemplatelistBean.ItemlistBean itemBean : bean.itemlist) {
                        if (itemBean.ids.equals(ids)) {
                            itemBean.value = value;
                        }
                    }
                }
                adapter.notifyDataSetChanged();
//                adapter.updateSingleRow(customListView, idOnMyClick);
                break;
            case "checkbox"://复选框
                //json字符串
                String jsonString = value;
                //去掉反斜杠
                jsonString = jsonString.replaceAll("\\\\", "");
                List<CheckBoxBean> checkBoxBeanList = GsonUtil.GsonToList(jsonString, CheckBoxBean.class);
                Intent intent = new Intent(TiJiaoXunChaShuJuActivity.this, CheckBoxActivity.class);
                intent.putExtra("ids", ids);
                intent.putExtra("data", (Serializable) checkBoxBeanList);
                JumpHelper.jumpForResult(TiJiaoXunChaShuJuActivity.this, intent, REQUEST_CODE_CHECKBOX, false);
                break;
            case "date"://选择日期
                //将日期转换成年月日
                value = Utils.getDateYearMonthDay(value);
                showDatePicker(ids, value);
                break;
            case "img"://上传图片
                //赋值动态图片 ids
                idsgDongtai = ids;
                //赋值动态图片 图片地址
                imgDongtai = value;
                openXiangceDongtaiImg();
                break;
            case "imgSign"://受检单位签章
                //赋值动态图片 ids
                idsgDongtai = ids;
                //赋值动态图片 图片地址
                imgDongtai = value;
                Intent intentSign = new Intent(TiJiaoXunChaShuJuActivity.this, QianZiActivity.class);
                JumpHelper.jumpForResult(TiJiaoXunChaShuJuActivity.this, intentSign, REQUEST_CODE_IMGSIGN, false);
                break;

            case "radioKey"://radio text文案 文字过多点击用对话框显示
                CommonQueDingDialog dingDialog = new CommonQueDingDialog(TiJiaoXunChaShuJuActivity.this, "详细内容", value);
                dingDialog.show();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    //动态列表被点击了
    @Subscriber(tag = "BianJiZiLiaoActivity.refreshgDongtaiIds")
    private void refreshgDongtaiIds(Intent intent) {
        idsgDongtai = intent.getStringExtra("ids");
    }
}
